package com.example.appointment;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.appointment.meta.QQLoginVO;
import com.example.appointment.meta.QQUserVO;
import com.example.appointment.util.SharedPrefsUtil;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.liulishuo.filedownloader.FileDownloader;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.tauth.Tencent;

import org.litepal.LitePalApplication;
import org.litepal.tablemanager.Connector;

/**
 * Created by hehe on 2016/8/30.
 */
public class AppointmentApplication extends LitePalApplication {

    public static AppointmentApplication sInstance;
    public static final String AppID = "222222";
    public static final String QQLOGININFO = "QQLOGININFO";
    public static final String QQUSERINFO = "QQUSERINFO";

    public Tencent getmTencent() {
        return mTencent;
    }

    protected Tencent mTencent;

    public static IWXAPI api;

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
//        LitePalApplication.initialize(this);如果已經繼承了其他的application
        SQLiteDatabase db = Connector.getDatabase();//初始化數據庫

        /**
         * 仅仅是缓存Application的Context，不耗时
         */
        FileDownloader.init(getContext());

//        //QQSDK初始化
//        mTencent = Tencent.createInstance(AppID,this);
        //微信SDK初始化
        //注册微信
        api = WXAPIFactory.createWXAPI(this, "你的应用在微信上申请的app_id", true);
        api.registerApp("你的应用在微信上申请的app_id");

    }

    /**
     * 获取imageloader实例
     *
     * @return
     */
    public static ImageLoader getImageLoader() {
        if (!ImageLoader.getInstance().isInited()) {
            initImageLoader();
        }
        return ImageLoader.getInstance();
    }

    public static AppointmentApplication getInstance() {
        if (sInstance == null) {
            throw new NullPointerException("app not created....");
        }

        return sInstance;
    }

    private static void initImageLoader() {

        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(true).cacheOnDisk(true)
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED).build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                sInstance).threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .diskCacheSize(50 * 1024 * 1024)
                // 50 Mb
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .defaultDisplayImageOptions(options).writeDebugLogs() // Remove
                // for
                // release
                // app
                .build();

        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config);
    }


    //获取QQ登陆信息
    public void setQQLogin(QQLoginVO qqLogin) {
        if (qqLogin != null) {
            SharedPrefsUtil.putValue(getApplicationContext(), QQLOGININFO, new Gson().toJson(qqLogin));
        }
    }

    public QQLoginVO getQQLogin() {

        QQLoginVO qqLoginVO = null;
        try {
            qqLoginVO = new QQLoginVO();
            qqLoginVO = new Gson().fromJson(SharedPrefsUtil.getValue(getApplicationContext(), QQLOGININFO, "{}"), QQLoginVO.class);
        } catch (JsonSyntaxException e) {
        }
        return qqLoginVO;
    }

    //获取QQ用户信息
    public void setQQUserVO(QQUserVO qqUserVO) {
        if (qqUserVO != null) {
            SharedPrefsUtil.putValue(getApplicationContext(), QQUSERINFO, new Gson().toJson(qqUserVO));
        }
    }

    public QQUserVO getQQUserVO() {

        QQUserVO qqUserVO = null;
        try {
            qqUserVO = new QQUserVO();
            qqUserVO = new Gson().fromJson(SharedPrefsUtil.getValue(getApplicationContext(), QQUSERINFO, "{}"), QQUserVO.class);
        } catch (JsonSyntaxException e) {
        }
        return qqUserVO;
    }

    /**
     * 退出登陆
     */
    public void exitLogin() {
        if (mTencent != null) {
            //注销登录
            mTencent.logout(getApplicationContext());
            SharedPrefsUtil.removeValue(getApplicationContext(), QQUSERINFO);
            SharedPrefsUtil.removeValue(getApplicationContext(), QQLOGININFO);
        }
    }


}
